/**
 * 
 */
package com.jiang.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.jiang.dao.AbstractDAOImpl;
import com.jiang.dao.IRoleDAO;
import com.jiang.vo.Action;
import com.jiang.vo.Groups;
import com.jiang.vo.Role;

/**
 * @author Administrator
 *
 */
public class RoleDAOImpl extends AbstractDAOImpl<Integer> implements IRoleDAO {

	@Override
	public Boolean doCreate(Role vo) throws SQLException {
		return null;
	}

	@Override
	public Boolean doRemove(Role vo) throws SQLException {
		return null;
	}

	@Override
	public Boolean doRemoveBatch(Set<Integer> ids) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see com.jiang.dao.IDAO#doUpdate(java.lang.Object)
	 */
	@Override
	public Boolean doUpdate(Role vo) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Role doFindById(Integer id) throws SQLException {
		return null;
	}

	@Override
	public List<Role> doFindAll() throws SQLException {
		return null;
	}

	@Override
	public List<Role> doFindAllSplit(Integer currentPage, Integer lineSize) throws SQLException {
		return null;
	}

	@Override
	public List<Role> doFindAllSplit(Integer currentPage, Integer lineSize, String column, String keyWord)
			throws SQLException {
		return null;
	}

	@Override
	public Integer getAllCount() throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer getAllCount(String column, String keyWord) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see com.jiang.dao.IRoleDAO#findGroupsByRId(java.lang.Integer)
	 */
	@Override
	public void doFindGroupsByRole(Role role) throws SQLException {
		
		String sql = " SELECT re.gid,title,note FROM groups ,(SELECT rid,gid FROM role_groups WHERE rid= ? )"
				+ " re WHERE groups.gid=re.gid;  ";
		super.ps = super.conn.prepareStatement(sql);
		super.ps.setInt(1, role.getRid());
		ResultSet rs = super.ps.executeQuery();
		while(rs.next()){
			Groups g = new Groups();
			g.setGid(rs.getInt(1));
			g.setTitle(rs.getString(2));
			g.setNote(rs.getString(3));
			role.getGroups().add(g);
		}
	}

	/* (non-Javadoc)
	 * @see com.jiang.dao.IRoleDAO#findActionsByRId(java.lang.Integer)
	 */
	@Override
	public List<Action> doFindActionsByRole(Role role) throws SQLException {
		// 权限组对应的权限，使用分组查询，然后赛回到各个权限组当中
		List<Action> actions = new ArrayList<Action>();
		List<Groups> groupsAll = role.getGroups();
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT actid,title,url,gid FROM action WHERE gid IN( ");
		for(int i=0;i<groupsAll.size();i++){
			sb.append(groupsAll.get(i).getGid()+",");
		}
		sb.delete(sb.length()-1, sb.length());
		sb.append(") ");
		String sql = sb.toString();
		super.ps = super.conn.prepareStatement(sql);
		ResultSet rs = super.ps.executeQuery();
		while(rs.next()){
			Action action = new Action();
			action.setActid(rs.getInt(1));
			action.setTitle(rs.getString(2));
			action.setUrl(rs.getString(3));
			int gid = rs.getInt(4);
			for(int i=0;i<groupsAll.size();i++){
				if(gid == groupsAll.get(i).getGid()){
					// 因为每个权限只属于一个权限组
					groupsAll.get(i).getActions().add(action);
					break;
				}
			}
//			Groups group = new Groups();
//			group.setGid(rs.getInt(4));
//			action.setGroups(group);
			actions.add(action);
		}
		
		return actions;
	}

}
